import { createBrowserRouter, createHashRouter } from 'react-router-dom';

import lazyLoadComp from '@/router/lazyComp';
import homeRouter from '@/router/modules/homeRouter';
import publicRouter from '@/router/modules/publicRouter';

const NotFound = lazy(() => import('@/pages/exception/404'));

function removeTrailingSlash(str: string) {
	console.log('[ str ] >', str);
	return str && str.endsWith('/') && str.length > 1 ? str.slice(0, -1) : str;
}

export const routersBrowser = createBrowserRouter(
	[...publicRouter, ...homeRouter, { path: '*', element: lazyLoadComp(NotFound) }],
	{
		basename: removeTrailingSlash(import.meta.env.VITE_BASE_PATH),
		patchRoutesOnNavigation: ({ patch, path }) => {
			// if (getIsNeedPatch(path)) {
			//   isAlreadyPatch = true;

			//   await initAuthRoutes(patch);
			// }
			console.log('[ patchRoutesOnNavigation ] >', patch, path);
		}
	}
);
const routers = createHashRouter(
	[...publicRouter, ...homeRouter, { path: '*', element: lazyLoadComp(NotFound) }],
	{
		// HashRouter 不需要 basename，一般都从 # 开始
		patchRoutesOnNavigation: ({ patch, path }) => {
			console.log('[ patchRoutesOnNavigation ] >', patch, path);
		}
	}
);

export default routers;
